#! /usr/bin/env python3
# Copyright (c) 2013 Christopher Nelson <nadiasvertex@gmail.com>
# Copyright (c) 2013 Mark Travis <mtravis15432+src@gmail.com>
# All rights reserved. No warranty, explicit or implicit, provided.
#
# This file is part of InfiniSQL (tm). It is available either under the
# GNU Affero Public License or under a commercial license. Contact the
# copyright holder for information about a commercial license if terms
# of the GNU Affero Public License do not suit you.
#
# This copy of InfiniSQL is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# InfiniSQL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with InfiniSQL. It should be in the top level of the source
# directory in a file entitled "COPYING".
# If not, see <http://www.gnu.org/licenses/>.

import os

cheader_top = """#ifndef CFGENUM_H
#define CFGENUM_H

/* Automatically generated by gen_cfg_enum.py: do not edit by hand */

enum cfgenum_e
{
"""

cheader_bot = """

#endif //CFGENUM_H
"""

pheader_top = """#! /usr/bin/env python3

# Automatically generated by gen_cfg_enum.py: do not edit by hand

"""

cfg_dict = {
    'CMDNONE': 0,
    'CMDOK': 1,
    'CMDNOTOK': 2,
    'CMDGET': 3,
    'CMDSET': 4,
    'CMDANONYMOUSPING': 5,
    'CMDBADLOGINMESSAGES': 6,
    'CMDSTART': 7,
    'CMDSTOP': 8,
    'CMDLISTENER': 9,
    'CMDUSERSCHEMAMGR': 11,
    'CMDDEADLOCKMGR': 12,
    'CMDTRANSACTIONAGENT': 13,
    'CMDENGINE': 14,
    'CMDOBGATEWAY': 15,
    'CMDIBGATEWAY': 16,
    'CMDGLOBALCONFIG': 17,
    'CMDLOCALCONFIG': 18,
    'CMDGETTOPOLOGYMGRMBOXPTR': 19,
    'CMDOBGATEWAY': 20,
    'CMDPGHANDLER': 21
}

actor_types_dict = {
    'ACTOR_NONE': 0,
    'ACTOR_TOPOLOGYMGR': 1,
    'ACTOR_TRANSACTIONAGENT': 3,
    'ACTOR_ENGINE': 4,
    'ACTOR_USERSCHEMAMGR': 5,
    'ACTOR_DEADLOCKMGR': 6,
    'ACTOR_OBGATEWAY': 7,
    'ACTOR_IBGATEWAY': 8,
    'ACTOR_LISTENER': 9,
    'ACTOR_PGHANDLER': 10
}

firstactorid = 100

with open('infinisql_cfgenum.h', 'w') as cheader:
    cheader.write(cheader_top)
    items = sorted([(v,k) for k,v in cfg_dict.items()])
    enum_lines = ['    %s=%s' % (k, v) for v, k in items]
    enum_body = ",\n".join(enum_lines)
    cheader.write(enum_body)
    cheader.write('\n};\n\n')
    cheader.write('enum actortypes_e\n{\n')

    items = sorted([(v,k) for k,v in actor_types_dict.items()])
    actor_lines = ['    %s=%s' % (k, v) for v, k in items]
    actor_body = "\n".join(actor_lines)
    cheader.write(actor_body)
    cheader.write('\n};\n\n')
    cheader.write('#define FIRSTACTORID ' + str(firstactorid + 1))
    cheader.write(cheader_bot)

cfg_path = os.path.join(os.path.dirname(__file__), "engine", "cfg.py")
with open(cfg_path, 'w') as pheader:
    pheader.write(pheader_top)
    items = sorted([(v,k) for k,v in cfg_dict.items()])
    config_lines = ['%s=%s' % (k.replace("CMD", "CMD_"), v) for v, k in items]
    config_body = "\n".join(config_lines)
    pheader.write(config_body)
    pheader.write("\n\n")

    items = sorted([(v,k) for k,v in actor_types_dict.items()])
    actor_lines = ['%s=%s' % (k, v) for v, k in items]
    actor_body = "\n".join(actor_lines)
    pheader.write(actor_body)
    pheader.write("\n\n")

    pheader.write('firstactorid = ')
    pheader.write(str(firstactorid))
    pheader.write('\n')
